package com.zhongshengnetwork.rightbe.lang.model;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.cardview.widget.CardView;
import androidx.core.content.ContextCompat;
import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.Glide;
import com.tencent.connect.share.QQShare;
import com.tencent.connect.share.QzoneShare;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXTextObject;
import com.zhongshengnetwork.rightbe.Adv.model.AdvertUtils;
import com.zhongshengnetwork.rightbe.Adv.model.SPManageUtils;
import com.zhongshengnetwork.rightbe.Constant.APIKey;
import com.zhongshengnetwork.rightbe.Constant.Constant;
import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.common.AppThemeUtils;
import com.zhongshengnetwork.rightbe.common.BroadcastDefine;
import com.zhongshengnetwork.rightbe.common.CircleTransform;
import com.zhongshengnetwork.rightbe.common.CommonUtils;
import com.zhongshengnetwork.rightbe.common.GsonTools;
import com.zhongshengnetwork.rightbe.common.ScreenUtils;
import com.zhongshengnetwork.rightbe.common.TextUtil.DisplayUtils;
import com.zhongshengnetwork.rightbe.common.TextUtil.FontUitls;
import com.zhongshengnetwork.rightbe.common.TextUtil.ShowAllSpan;
import com.zhongshengnetwork.rightbe.common.TextUtil.ShowAllTextView;
import com.zhongshengnetwork.rightbe.common.TextUtil.VerticalTextView;
import com.zhongshengnetwork.rightbe.common.customview.CustomDialog;
import com.zhongshengnetwork.rightbe.common.customview.ShareDialog;
import com.zhongshengnetwork.rightbe.common.love.Love;
import com.zhongshengnetwork.rightbe.common.utils.ToastUtil;
import com.zhongshengnetwork.rightbe.gsonmodel.CollectModel;
import com.zhongshengnetwork.rightbe.gsonmodel.CommonModel;
import com.zhongshengnetwork.rightbe.gsonmodel.DryCommentModel;
import com.zhongshengnetwork.rightbe.gsonmodel.LangInfoModel;
import com.zhongshengnetwork.rightbe.gsonmodel.WeiShuContentDetailModel;
import com.zhongshengnetwork.rightbe.https.HttpCallBack;
import com.zhongshengnetwork.rightbe.https.HttpsUtils;
import com.zhongshengnetwork.rightbe.lang.activity.BookmarkActivity;
import com.zhongshengnetwork.rightbe.lang.activity.BookmarkCardActivity;
import com.zhongshengnetwork.rightbe.lang.activity.BookmarkListActivity;
import com.zhongshengnetwork.rightbe.lang.activity.LangCommentActivity;
import com.zhongshengnetwork.rightbe.lang.activity.LangDetailActivity;
import com.zhongshengnetwork.rightbe.lang.res.Configs;
import com.zhongshengnetwork.rightbe.login.LoginActivity;
import com.zhongshengnetwork.rightbe.wzt.activity.WZTContentActivity;
import com.zhongshengnetwork.rightbe.wzt.view.OnDoubleClickListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static android.content.Context.CLIPBOARD_SERVICE;

public class BookmarkCardViewAdapter extends PagerAdapter {

    public List<CollectModel> list;
    private Context context;
    private LayoutInflater inflater;

    //微信分享
    private List<String> nameList;
    private CollectModel langInfoModel = null;
    private BookmarkCardActivity cardViewActivity;
    private int screenWidth = 0;
    private int screenHeight = 0;
    private int textHeight = 0;
    private int maxLine = 0;
    private int imgW  = 0;
    private int imgH = 0;
    private ClipboardManager myClipboard;

    private void writeComment(int position){
        if (cardViewActivity != null){
            cardViewActivity.writeComment(position);
        }
    }

    public BookmarkCardViewAdapter(Context context, BookmarkCardActivity activity) {
        this.context = context;
        cardViewActivity = activity;
        this.screenWidth = ScreenUtils.getScreenWidth(context);
        this.screenHeight = ScreenUtils.getScreenHeight(context);
        this.imgW = this.screenWidth;// - ScreenUtils.dip2px(context, 40);
        int height = Math.round(imgW / Configs.Value.IMAGE_WIDTH_HEIGHT_RATIO);
        this.imgH = height;

        this.textHeight = this.screenHeight - DisplayUtils.dp2px(activity, 240) - this.imgH; //180
        int textLineHeight = DisplayUtils.sp2px(activity, 16) + DisplayUtils.dp2px(activity, 8);
        this.maxLine = this.textHeight/textLineHeight;
        if (this.maxLine > 1){
            this.maxLine -= 1;
        }

        this.list = new ArrayList<CollectModel>();
        inflater = LayoutInflater.from(context);
        myClipboard = (ClipboardManager)context.getSystemService(CLIPBOARD_SERVICE);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = inflater.inflate(R.layout.bookmark_card_item, container, false);
        container.addView(view);
        Love cardview_item_layout = (Love) view.findViewById(R.id.cardview_item_layout);
        ImageView cardview_item_bg = (ImageView) view.findViewById(R.id.cardview_item_bg);
        ShowAllTextView textView = (ShowAllTextView) view.findViewById(R.id.banner_title);

        VerticalTextView banner_title_vertical = (VerticalTextView) view.findViewById(R.id.banner_title_vertical);
        //banner_title_vertical.setTextColor(Color.parseColor("#97a1ac"));
        //banner_title_vertical.setFontSize(ScreenUtils.dip2px(cardViewActivity, 16));
        //banner_title_vertical.setLineWidth(ScreenUtils.dip2px(cardViewActivity, 30));
        RelativeLayout banner_title_vertical_layout = (RelativeLayout) view.findViewById(R.id.banner_title_vertical_layout);

        ImageView header = (ImageView) view.findViewById(R.id.banner_header);
        ImageView banner_img = (ImageView) view.findViewById(R.id.banner_img);
        LinearLayout banner_like_layout = (LinearLayout) view.findViewById(R.id.banner_like_layout);
        ImageView banner_like_icon = (ImageView) view.findViewById(R.id.banner_like_icon);
        TextView banner_like_count = (TextView) view.findViewById(R.id.banner_like_count);
        LinearLayout banner_collect_layout = (LinearLayout) view.findViewById(R.id.banner_collect_layout);
        ImageView banner_collect_icon = (ImageView) view.findViewById(R.id.banner_collect_icon);
        TextView banner_collect_count = (TextView) view.findViewById(R.id.banner_collect_count);
        LinearLayout banner_share_layout = (LinearLayout) view.findViewById(R.id.banner_share_layout);
        ImageView banner_share_icon = (ImageView) view.findViewById(R.id.banner_share_icon);
        TextView banner_share_count = (TextView) view.findViewById(R.id.banner_share_count);
        LinearLayout banner_comment_layout = (LinearLayout) view.findViewById(R.id.banner_comment_layout);
        ImageView banner_comment_icon = (ImageView) view.findViewById(R.id.banner_comment_icon);
        TextView banner_comment_count = (TextView) view.findViewById(R.id.banner_comment_count);
        CollectModel model = list.get(position);
        cardview_item_bg.setVisibility(View.GONE);

        if (imgW > 0 && imgH > 0){
            ViewGroup.LayoutParams params = banner_img.getLayoutParams();
            params.width = imgW;
            params.height = imgH;
            banner_img.setLayoutParams(params);

            params = header.getLayoutParams();
            params.width = imgH;
            params.height = imgH;
            header.setLayoutParams(params);
        }

        if (model.getImglist() != null && model.getImglist().size() > 0){
            header.setVisibility(View.INVISIBLE);
            banner_img.setVisibility(View.VISIBLE);
            //Glide.with(this.context).load(model.getImglist().get(0)).transform(new CircleTransform(this.context)).dontAnimate().into(header);
            if (this.context != null){
                Glide.with(this.context).load(model.getImglist().get(0)).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(banner_img);
                //Glide.with(this.context).load(model.getImglist().get(0)).apply(RequestOptions.bitmapTransform(new BlurTransformation(25, 3))).placeholder(R.mipmap.bg_home).dontAnimate().into(cardview_item_bg);
            }
        }else {
            header.setVisibility(View.VISIBLE);
            banner_img.setVisibility(View.INVISIBLE);
            if (this.context != null){
                if (position == 0){
                    header.setVisibility(View.INVISIBLE);
                    banner_img.setVisibility(View.VISIBLE);
                    Glide.with(this.context).load(cardViewActivity.bookmarkModel.getIcon()).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(banner_img);
                }else {
                    Glide.with(this.context).load(model.getHeader()).transform(new CircleTransform(this.context)).dontAnimate().into(header);
                }
                //Glide.with(this.context).load(model.getHeader()).apply(RequestOptions.bitmapTransform(new BlurTransformation(25, 3))).placeholder(R.mipmap.bg_home).dontAnimate().into(cardview_item_bg);
            }
        }

        if (this.maxLine > 0){
            textView.setMaxShowLines(this.maxLine);
        }else {
            textView.setMaxShowLines(FontUitls.getTextMaxLine(screenHeight, cardViewActivity));//这里根据手机屏幕做适配，默认10
        }
        textView.setMyText(model.getTitle());
        textView.setTag(position);
        textView.setClickable(true);
        textView.setFocusable(false);

        textView.setOnAllSpanClickListener(new ShowAllSpan.OnAllSpanClickListener() {
            @Override
            public void onClick(View widget) {
                //ToastUtil.show(context, "点击了更多");
                if ((int)widget.getTag() == 0){
                    Intent intent = new Intent(cardViewActivity , BookmarkListActivity.class);
                    Bundle bundle=new Bundle();
                    bundle.putSerializable("model", cardViewActivity.bookmarkModel);//序列化
                    intent.putExtras(bundle);//发送数据
                    cardViewActivity.startActivity(intent);
                    return;
                }
                collectShare((int)widget.getTag(), false);
            }
        });

        if (position > 0 && model.getTitle().length() < Constant.PortDisplayLength && FontUitls.isPortDisplay(model.getTitle()) == true){
            textView.setVisibility(View.INVISIBLE);
            banner_title_vertical_layout.setVisibility(View.VISIBLE);
            banner_title_vertical.setText(FontUitls.formatPortDisplay(model.getTitle()));
            /*
            banner_title_vertical.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    int p = (int)view.getTag();
                    if (list == null || position >= list.size()){
                        return false;
                    }
                    final CollectModel model = list.get(p);
                    CommonUtils.copy(cardViewActivity, model.getTitle(), model.isOriginal(), model.getSourceid());
                    return false;
                }
            });
            */

        }else {
            textView.setVisibility(View.VISIBLE);
            banner_title_vertical_layout.setVisibility(View.INVISIBLE);
            /*
            textView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    int p = (int)view.getTag();
                    if (list == null || position >= list.size()){
                        return false;
                    }
                    final CollectModel model = list.get(p);
                    CommonUtils.copy(cardViewActivity, model.getTitle(), model.isOriginal(), model.getSourceid());
                    return false;
                }
            });
            */
        }
        banner_title_vertical_layout.setTag(position);
        banner_title_vertical_layout.setOnTouchListener(new OnDoubleClickListener(new OnDoubleClickListener.DoubleClickCallback() {
            @Override
            public void onDoubleClick(View v, MotionEvent event) {
                if (cardViewActivity.love_layout != null){
                    //cardViewActivity.love_layout.addLoveView(screenWidth/2,screenHeight/2);
                    onClickLike((int)v.getTag());
                }
            }
        }));
        banner_title_vertical.setTag(position);
        banner_title_vertical.setOnTouchListener(new OnDoubleClickListener(new OnDoubleClickListener.DoubleClickCallback() {
            @Override
            public void onDoubleClick(View v, MotionEvent event) {
                if (cardViewActivity.love_layout != null){
                    //cardViewActivity.love_layout.addLoveView(screenWidth/2,screenHeight/2);
                    onClickLike((int)v.getTag());
                }
            }
        }));

        cardview_item_layout.setTag(position);
        cardview_item_layout.setClickable(true);
        cardview_item_layout.setFocusable(false);
        cardview_item_layout.setOnTouchListener(new OnDoubleClickListener(new OnDoubleClickListener.DoubleClickCallback() {
            @Override
            public void onDoubleClick(View v, MotionEvent event) {
                if (cardViewActivity.love_layout != null){
                    if((int)v.getTag() == 0){
                        return;
                    }
                    //cardViewActivity.love_layout.addLoveView(screenWidth/2,screenHeight/2);
                    onClickLike((int)v.getTag());
                }
            }
        }));

        cardview_item_layout.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                int p = (int)view.getTag();
                if (list == null || p >= list.size()){
                    return false;
                }
                if (p == 0){
                    return false;
                }
                final CollectModel model = list.get(p);
                CommonUtils.copy(cardViewActivity, model.getTitle(), model.isOriginal(), model.getSourceid());
                /*
                boolean isOpen = SPManageUtils.getInstance(CustomApplication.mContext).getSPBoolean(AdvertUtils.MoveCommentKey, true);
                if (isOpen == true){
                    SPManageUtils.getInstance(CustomApplication.mContext).putSPBoolean(AdvertUtils.MoveCommentKey, false);
                    cardViewActivity.displayComment();
                    ToastUtil.show(cardViewActivity, "动态评论墙已关闭");
                }else {
                    SPManageUtils.getInstance(CustomApplication.mContext).putSPBoolean(AdvertUtils.MoveCommentKey, true);
                    cardViewActivity.displayComment();
                    ToastUtil.show(cardViewActivity, "动态评论墙已开启");
                }
                */
                return false;
            }
        });

        CardView cardview = (CardView) view.findViewById(R.id.cardview);
        cardview.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());
        textView.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());

        LinearLayout comment_line = (LinearLayout) view.findViewById(R.id.comment_line);
        comment_line.setBackgroundColor(AppThemeUtils.getInstance().getCardBackgroundColor());
        RelativeLayout comment_layout = (RelativeLayout) view.findViewById(R.id.comment_layout);
        comment_layout.setBackgroundResource(AppThemeUtils.getInstance().getCommentBg());
        comment_layout.setClickable(true);
        comment_layout.setTag(position);
        comment_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                writeComment((int)view.getTag());
            }
        });
        TextView write_comment_text = (TextView) view.findViewById(R.id.write_comment_text);
        write_comment_text.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
        if (model.getCommentcount() == 0){
            write_comment_text.setText("抢沙发");
        }else {
            write_comment_text.setText("说点什么吧");
        }

        if (position == 0){
            banner_comment_layout.setVisibility(View.INVISIBLE);
            banner_like_layout.setVisibility(View.INVISIBLE);
            banner_collect_layout.setVisibility(View.INVISIBLE);
            banner_share_layout.setVisibility(View.INVISIBLE);
            comment_line.setVisibility(View.INVISIBLE);
            comment_layout.setVisibility(View.INVISIBLE);
        }else {
            banner_comment_layout.setVisibility(View.VISIBLE);
            banner_like_layout.setVisibility(View.VISIBLE);
            banner_collect_layout.setVisibility(View.VISIBLE);
            banner_share_layout.setVisibility(View.VISIBLE);
            comment_line.setVisibility(View.VISIBLE);
            comment_layout.setVisibility(View.VISIBLE);
        }

        if (model.getCommentcount() == 0){
            if (model.getType() == 0 || model.getType() == 3 || (!CommonUtils.isEmpty(model.getTypename()) && model.getTypename().contains("评论"))){
                //评论类型
                banner_comment_count.setText("回复");
            }else {
                banner_comment_count.setText("评论");
            }
        }else {
            banner_comment_count.setText(""+model.getCommentcount());
        }
        banner_comment_layout.setTag(position);
        banner_comment_layout.setClickable(true);
        banner_comment_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                collectShare((int)v.getTag(), false);
            }
        });

        //点赞
        if (model.getLikecount() == 0){
            banner_like_count.setText("笔芯");
        }else {
            banner_like_count.setText(""+model.getLikecount());
        }
        if (model.isLiked() == true){
            banner_like_icon.setImageResource(R.drawable.my_like_select);
            banner_like_count.setTextColor(ContextCompat.getColor(cardViewActivity,R.color.lightGreenColor));
        }else {
            banner_like_icon.setImageResource(R.drawable.my_like);
            banner_like_count.setTextColor(ContextCompat.getColor(cardViewActivity,R.color.defaultSmallFontColor));
        }
        banner_like_layout.setTag(position);
        banner_like_layout.setClickable(true);
        banner_like_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickLike((int)v.getTag());
            }
        });

        if (CommonUtils.isLogin() == true && cardViewActivity.bookmarkModel.getUserid().equals(CustomApplication.loginModel.getUid())){
            //收藏夹的作者显示删除
            banner_collect_icon.setImageResource(R.drawable.my_delete);
            banner_collect_count.setText("删除");
        }else {
            if (model.getCollectcount() == 0){
                banner_collect_count.setText("收藏");
            }else {
                banner_collect_count.setText(""+model.getCollectcount());
            }
            if (model.isCollected() == true){
                banner_collect_icon.setImageResource(R.drawable.my_collect_select);
                banner_collect_count.setTextColor(ContextCompat.getColor(cardViewActivity,R.color.lightGreenColor));
            }else {
                banner_collect_icon.setImageResource(R.drawable.my_collection);
                banner_collect_count.setTextColor(ContextCompat.getColor(cardViewActivity,R.color.defaultSmallFontColor));
            }
        }

        banner_collect_layout.setTag(position);
        banner_collect_layout.setClickable(true);
        banner_collect_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickCollect((int)v.getTag());
            }
        });


        banner_share_count.setText(""+model.getSharecount());
        if (!CommonUtils.isEmpty(model.getTypename()) && model.getTypename().contains("评论")){
            banner_share_icon.setImageResource(R.drawable.my_details);
            banner_share_count.setText("详情");
        }else {
            banner_share_icon.setImageResource(R.drawable.my_share);
            if (model.getSharecount() == 0){
                banner_share_count.setText("分享");
            }
        }

        banner_share_layout.setTag(position);
        banner_share_layout.setClickable(true);
        banner_share_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                collectShare((int)v.getTag(), true);
            }
        });

        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;//更新数据
    }

    //加入收藏夹
    private void bookmark(int id){
        Intent intent = new Intent(cardViewActivity, BookmarkActivity.class);
        intent.putExtra("id",id);
        cardViewActivity.startActivity(intent);
    }

    private void onClickCopy(final int index, boolean isShare){
        if (list == null || list.size() == 0){
            return;
        }
        final CollectModel model = list.get(index);
        if (CommonUtils.isEmpty(model.getTitle())){
            return;
        }
        try{
            ClipData myClip = ClipData.newPlainText("text", model.getTitle());
            myClipboard.setPrimaryClip(myClip);
            if (isShare == true){
                Toast.makeText(cardViewActivity, "文字已复制，分享启动中...",
                        Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(cardViewActivity, "文字已复制~",
                        Toast.LENGTH_SHORT).show();
            }
        }catch (Exception e){

        }
    }

    //进入分享详情
    private void collectShare(final int index, boolean share){

        final CollectModel model = list.get(index);
        if (model.getType() == 0){//评论

            final HashMap<String, String> map = new HashMap<String, String>();
            map.put(APIKey.idKey, model.getSourceid());
            map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
            String url = "miniapp/langappid/commentcontent.do";
            HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                @Override
                public void onSuccess(String data) {

                    CommonModel commonModel = GsonTools.getCommonModel(data);
                    if (commonModel.getFlag().equals("1")){
                        DryCommentModel model = GsonTools.getCommentModel(data);
                        Intent intent = new Intent(cardViewActivity, LangCommentActivity.class);
                        intent.putExtra("id",model.getSourceid());
                        intent.putExtra("isShare",false);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("DryCommentModel", model);
                        intent.putExtras(bundle);
                        cardViewActivity.startActivity(intent);
                    }else {
                        CustomApplication.showTip(commonModel, cardViewActivity);
                    }
                }

                @Override
                public void onError(String msg) {

                }
            });
        }else if(model.getType() == 1){
            //微句
            final HashMap<String, String> map = new HashMap<String, String>();
            map.put(APIKey.idKey, model.getSourceid());
            map.put(APIKey.tokenKey,CustomApplication.loginModel.getToken());
            String url = "miniapp/langappid/getlanginfo.do";
            HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                @Override
                public void onSuccess(String data) {
                    CommonModel commonModel = GsonTools.getCommonModel(data);
                    if (commonModel.getFlag().equals("1")){
                        LangInfoModel langInfoModel = GsonTools.getLangInfoModel(data);

                        if (langInfoModel.getStatus() == 1){
                            Intent intent = new Intent(cardViewActivity, LangDetailActivity.class);
                            Bundle bundle=new Bundle();
                            bundle.putSerializable("model", langInfoModel);//序列化
                            intent.putExtras(bundle);//发送数据
                            intent.putExtra("share",share);
                            cardViewActivity.startActivity(intent);
                        }
                    }else {
                        CustomApplication.showTip(commonModel, cardViewActivity);
                    }
                }

                @Override
                public void onError(String msg) {

                }
            });
        }else if (model.getType() == 2){
            //微纸条内容
            final HashMap<String, String> map = new HashMap<String, String>();
            map.put(APIKey.idKey, model.getSourceid());
            map.put(APIKey.tokenKey,CustomApplication.loginModel.getToken());
            String url = "weishu/content/info.do";
            HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                @Override
                public void onSuccess(String data) {
                    CommonModel commonModel = GsonTools.getCommonModel(data);
                    if (commonModel.getFlag().equals("1")){
                        WeiShuContentDetailModel langInfoModel = GsonTools.getWeiShuContentDetailModel(data);
                        Intent intent = new Intent(cardViewActivity, WZTContentActivity.class);
                        Bundle bundle=new Bundle();
                        bundle.putSerializable("model", langInfoModel);//序列化
                        intent.putExtra("share",share);
                        intent.putExtras(bundle);//发送数据
                        cardViewActivity.startActivity(intent);
                    }else {
                        CustomApplication.showTip(commonModel, cardViewActivity);
                    }
                }

                @Override
                public void onError(String msg) {
                }
            });
        }else if (model.getType() == 3){
            //微纸条评论
            final HashMap<String, String> map = new HashMap<String, String>();
            map.put(APIKey.idKey, model.getSourceid());
            map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
            String url = "weishu/theme/comment/detail.do";
            HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                @Override
                public void onSuccess(String data) {
                    CommonModel commonModel = GsonTools.getCommonModel(data);
                    if (commonModel.getFlag().equals("1")){
                        DryCommentModel model = GsonTools.getCommentModel(data);
                        Intent intent = new Intent(cardViewActivity,LangCommentActivity.class);
                        intent.putExtra("id",model.getSourceid());
                        intent.putExtra("type",1);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("DryCommentModel", model);
                        intent.putExtras(bundle);
                        cardViewActivity.startActivity(intent);
                    }else {
                        CustomApplication.showTip(commonModel, cardViewActivity);
                    }
                }

                @Override
                public void onError(String msg) {

                }
            });
        }
    }

    private void onClickLike(final int index){

        if (CommonUtils.isLogin() == false){
            Intent intent = new Intent(cardViewActivity, LoginActivity.class);
            cardViewActivity.startActivity(intent);
            return;
        }

        final CollectModel model = list.get(index);
        if (model.isLiked() == true){
            if (model.getLikecount() > 0){
                model.setLikecount(model.getLikecount()-1);
            }
            model.setLiked(false);
        }else {
            model.setLikecount(model.getLikecount()+1);
            model.setLiked(true);
            if (cardViewActivity.love_layout != null){
                cardViewActivity.love_layout.addLoveView(screenWidth/2,screenHeight/2);
            }
        }

        notifyDataSetChanged();
        if (model.getType() == 0){
            //评论
            final Map<String,String> map = new HashMap<String, String>();
            map.put(APIKey.tokenKey,CustomApplication.loginModel.getToken());
            map.put(APIKey.idKey,model.getSourceid());
            String url = "miniapp/"+"langappid"+"/langlikecomment.do";
            HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                @Override
                public void onSuccess(String data) {
                }

                @Override
                public void onError(String msg) {

                }
            });
        }else if (model.getType() == 1){
            //微句
            if (model.isLiked() == true){
                final HashMap<String, String> map = new HashMap<String, String>();
                map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
                map.put("id", model.getSourceid());
                map.put(APIKey.typeKey,"1");
                map.put("answerid","0");
                String url = "miniapp/"+"langappid"+"/langcl.do";
                HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                    @Override
                    public void onSuccess(String data) {
                        Log.e("TAG","笔芯结果："+data);
                        //initLang();
                    }

                    @Override
                    public void onError(String msg) {
                    }
                });
            }else {
                final HashMap<String, String> map = new HashMap<String, String>();
                map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
                map.put("id", model.getSourceid());
                String url = "miniapp/"+"langappid"+"/canclelike.do";
                HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                    @Override
                    public void onSuccess(String data) {
                        Log.e("TAG","笔芯结果："+data);
                    }

                    @Override
                    public void onError(String msg) {
                    }
                });
            }
        }else if (model.getType() == 2){
            //微纸条内容
            final HashMap<String, String> map = new HashMap<String, String>();
            map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
            map.put("id", model.getSourceid());
            String url = "weishu/content/like.do";
            HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                @Override
                public void onSuccess(String data) {
                    Log.e("TAG","笔芯结果："+data);
                    //initLang();
                }

                @Override
                public void onError(String msg) {
                }
            });
        }else if (model.getType() == 3){
            //微纸条评论
            final Map<String,String> map = new HashMap<String, String>();
            map.put(APIKey.tokenKey,CustomApplication.loginModel.getToken());
            map.put(APIKey.idKey,model.getSourceid());
            String url = "weishu/theme/comment/like.do";
            HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                @Override
                public void onSuccess(String data) {
                }

                @Override
                public void onError(String msg) {

                }
            });
        }
    }

    private void onClickCollect(final int index){

        if (CommonUtils.isLogin() == false){
            Intent intent = new Intent(cardViewActivity, LoginActivity.class);
            cardViewActivity.startActivity(intent);
            return;
        }

        final CollectModel model = list.get(index);
        if (CommonUtils.isLogin() == true && cardViewActivity.bookmarkModel.getUserid().equals(CustomApplication.loginModel.getUid())){
            //作者删除
            CustomDialog dialog = null;
            final CustomDialog.Builder builder = new CustomDialog.Builder(cardViewActivity);
            builder.setTitle("温馨提示");
            builder.setMessage("确定要从本收藏夹中删除吗？");
            builder.setPositiveButton("删除", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            final HashMap<String, String> map = new HashMap<String, String>();
                            String url = "bookmark/content/delete.do";
                            map.put(APIKey.tokenKey, CommonUtils.formatString(CustomApplication.loginModel.getToken()));
                            map.put("bookmarkId", "" + cardViewActivity.bookmarkModel.getBookmarkId()+"");
                            map.put("contentId", "" + model.getId());
                            HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                                @Override
                                public void onSuccess(String data) {
                                    CommonModel commonModel = GsonTools.getCommonModel(data);
                                    cardViewActivity.runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            if (commonModel.getFlag().equals("1")){
                                                ToastUtil.show(cardViewActivity, "删除成功");
                                                list.remove(model);
                                                notifyDataSetChanged();
                                            }else {
                                                CustomApplication.showTip(commonModel, cardViewActivity);
                                            }
                                        }
                                    });
                                }

                                @Override
                                public void onError(String msg) {
                                    ToastUtil.show(cardViewActivity, "删除失败，请稍后尝试~");
                                }
                            });
                        }
                    }){

                    }.start();
                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            dialog = builder.create();
            dialog.show();

        }else {
            if (model.isCollected() == true){
                model.setCollected(false);
                model.setCollectcount(model.getCollectcount()-1);
            }else {
                model.setCollected(true);
                model.setCollectcount(model.getCollectcount()+1);
            }
            notifyDataSetChanged();
            if (model.getType() == 0){
                //评论
                final HashMap<String, String> map = new HashMap<String, String>();
                map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
                map.put("sourceid", model.getSourceid());
                map.put(APIKey.typeKey,"0");
                String url = "miniapp/"+"langappid"+"/addcollection.do";
                HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                    @Override
                    public void onSuccess(String data) {
                        Log.e("TAG","收藏结果："+data);
                    }

                    @Override
                    public void onError(String msg) {
                    }
                });
            }else if (model.getType() == 1){
                //微句
                if (model.isCollected() == true){
                    final HashMap<String, String> map = new HashMap<String, String>();
                    map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
                    map.put("sourceid", model.getSourceid());
                    map.put(APIKey.typeKey,"1");
                    String url = "miniapp/"+"langappid"+"/addcollection.do";
                    HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                        @Override
                        public void onSuccess(String data) {
                            Log.e("TAG","收藏结果："+data);
                        }

                        @Override
                        public void onError(String msg) {
                        }
                    });
                }else {
                    final HashMap<String, String> map = new HashMap<String, String>();
                    map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
                    map.put("id", model.getSourceid());
                    String url = "miniapp/"+"langappid"+"/cancleCollect.do";
                    HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                        @Override
                        public void onSuccess(String data) {
                            Log.e("TAG","笔芯结果："+data);
                        }

                        @Override
                        public void onError(String msg) {
                        }
                    });
                }
            }else if (model.getType() == 2){
                //微纸条内容
                final HashMap<String, String> map = new HashMap<String, String>();
                map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
                map.put("sourceid", model.getSourceid());
                map.put(APIKey.typeKey,"2");
                String url = "miniapp/"+"langappid"+"/addcollection.do";
                HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                    @Override
                    public void onSuccess(String data) {
                        Log.e("TAG","收藏结果："+data);
                    }

                    @Override
                    public void onError(String msg) {
                    }
                });
            }else if (model.getType() == 3){
                //微纸条评论
                final HashMap<String, String> map = new HashMap<String, String>();
                map.put(APIKey.tokenKey, CustomApplication.loginModel.getToken());
                map.put("sourceid", model.getSourceid());
                map.put(APIKey.typeKey,"3");
                String url = "miniapp/"+"langappid"+"/addcollection.do";
                HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                    @Override
                    public void onSuccess(String data) {
                        Log.e("TAG","收藏结果："+data);
                    }

                    @Override
                    public void onError(String msg) {
                    }
                });
            }
        }
    }

}
